package com.study.selection.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.selection.model.*;
import com.study.selection.service.*;
import com.study.selection.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/t_teacher")
public class T_TeacherController {
    // 注入Service
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacheringService teacheringService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SelectService selectService;

    @Autowired
    private ClassesService classesService;
    @Autowired
    private CollegeService collegeService;

    @Autowired
    private NewsService newsService;


    //获取任课信息
    @RequestMapping("/myCourse")
    public String getMyCourse(Integer teacherId,Model model){
        List<Course> list=courseService.getByTeacherId(teacherId);
        model.addAttribute("courseList",list);
        return "/view/teacher/class.jsp";
    }
    //获取所有学生成绩
    @RequestMapping("/courseGrade")
    public String getCourseSelection(Model model, HttpSession session){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        List<Select> list=selectService.getByTeacherId(teacher.getTeacherId());
        model.addAttribute("selectList",list);
        return "/view/teacher/chengji.jsp";
    }
    //获取选中学生成绩信息
    @RequestMapping("/getOne")
    public String getGrade(Integer selectId, Model model){
        Select select=selectService.getBySelectId(selectId);
        model.addAttribute("select",select);
        return "/view/teacher/chengjiEdit.jsp";
    }

    //获取选中课程详细信息
    @RequestMapping("/getCourse")
    public String getCourse(Integer courseId,Model model){
        Course course=courseService.getByCourseId(courseId);
        model.addAttribute("course",course);
        return "/view/teacher/classEdit.jsp";
    }
    //调课申请
    @RequestMapping("/updateCourse")
    public String updateCourse(Teachering teachering ,Model model){
        boolean result=teacheringService.addTeachering(teachering);
        if(result){
            model.addAttribute("message","调课申请成功");
        }else{
            model.addAttribute("message","调课申请失败");
        }
        return "/view/teacher/class.jsp";
    }
    //成绩录入
    @RequestMapping("/updateGrade")
    public String editGrade(Integer selectId,Integer grade){
        Select select=selectService.getBySelectId(selectId);
        select.setGrade(grade);
        boolean result=selectService.updateSelect(select);
        return "redirect:courseGrade";
    }
    //获取全部学生
    @RequestMapping("/getMyStudent")
    public String getMyCourseSelection(Integer teacherId, Model model){
        List<Select> list=selectService.getByTeacherId(teacherId);
        model.addAttribute("selectList",list);
        List<Classes> classesList=classesService.findAll();
        model.addAttribute("classesList",classesList);
        List<College> collegeList=collegeService.findAll();
        model.addAttribute("collegeList",collegeList);
        return "/view/teacher/student.jsp";
    }

    //修改密码
    @RequestMapping("/updatePassword")
    public String updatePassword(String oldPwd, String newPwd1, Model model, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        if (teacher.getPassword().equals(MD5Utils.getMD5(oldPwd))) {
            teacher.setPassword(MD5Utils.getMD5(newPwd1));
            teacherService.updateTeacher(teacher);
            model.addAttribute("result", "密码修改成功!");
            return "/view/teacher/updatePwdAgain.jsp";
        } else {
            model.addAttribute("result", "与原密码不匹配!");
            return "/view/teacher/updatePwdAgain.jsp";
        }
    }
    //获取新闻
    @RequestMapping("/listAllNews")
    public String doNewsList(@RequestParam(value = "page",defaultValue = "1") Integer page, Model model){
        //添加分页，page表示查询第几页
        PageHelper.startPage(page,5);
        List<News> list = newsService.findAll();
        PageInfo<News> info = new PageInfo<>(list,3);
        // 向页面返回封装后的数据
        model.addAttribute("info",info);
        return "/view/teacher/xinwen.jsp";
    }
}



